﻿using GalaSoft.MvvmLight;
using Microsoft.Xna.Framework.Input.Touch;
using Helper;
using System;
using System.Windows;
using Model;
using System.Linq;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Messaging;
using MvvmLightNavgation;

namespace DBFM7.ViewModel
{
    /// <summary>
    /// ChannelTile ViewModel
    /// createby:kklldog
    /// date:20120220
    /// </summary>
    public class ChannelTileViewModel : ViewModelBase,INavigation
    {
        /// <summary>
        /// Initializes a new instance of the ChannelTileViewModel class.
        /// </summary>
        public ChannelTileViewModel()
        {

            NavigationHelper.NavigatedMsgReg(this);

            this.TapCommand = new RelayCommand<string>(this.HubTile_Tap);
            this.ChannelSelectCancelCommand = new RelayCommand(this.NavigateToMainPage);

            this._FAVTileModel = new TileModel();
            this._HanYTileModel = new TileModel();
            this._HYTileModel = new TileModel();
            this._OMTileModel = new TileModel();
            this._RYTileModel = new TileModel();
            this._YYTileModel = new TileModel();
            
            if (!PlayListHelper.IsInited)
            {
                PlayListHelper.InitChannelComplete += new Action(PlayListHelper_InitChannelComplete);
                PlayListHelper.InitChannel();//初始化播放列表
            }
            else
                this.IsShowGoBtn = Visibility.Visible;
        }

        private string _Title="频道正在初始化...";
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        { get { return _Title; } 
          set 
          {
              this._Title = value;
              this.RaisePropertyChanged("Title");
          } 
        }


        private Visibility _IsShowGoBtn=Visibility.Collapsed;
        /// <summary>
        /// 是否显示右边的箭头
        /// </summary>
        public Visibility IsShowGoBtn
        {
            get { return this._IsShowGoBtn; }
            set
            {
                this._IsShowGoBtn = value;
                this.RaisePropertyChanged("IsShowGoBtn");
            }
        }

        /// <summary>
        /// 初始化tile的image跟title
        /// </summary>
        protected void PlayListHelper_InitChannelComplete()
        {
            var list = PlayListHelper.GetEachChannel1stSong();//获取所有频道的第一首歌曲
            var song = list.Where(s => s.channel == (int)ChannelType.华语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                this.HYTileModel.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                this.HYTileModel.Title = ChannelType.华语.ToString();
                this.HYTileModel.Message = s.artist + "-" + s.title;
            }
            else
            {
                this.HYTileModel.Title = ChannelType.华语.ToString();
                this.HYTileModel.Message = "No Song!";
            }


            song = list.Where(s => s.channel == (int)ChannelType.欧美).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                this.OMTileModel.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                this.OMTileModel.Title = ChannelType.欧美.ToString();
                this.OMTileModel.Message = s.artist + "-" + s.title;
            }
            else
            {
                this.OMTileModel.Title = ChannelType.欧美.ToString();
                this.OMTileModel.Message = "No Song!";
            }


            song = list.Where(s => s.channel == (int)ChannelType.韩语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                this.HanYTileModel.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                this.HanYTileModel.Title = ChannelType.韩语.ToString();
                this.HanYTileModel.Message = s.artist + "-" + s.title;
            }
            else
            {
                this.HanYTileModel.Title = ChannelType.韩语.ToString();
                this.HanYTileModel.Message = "No Song!";
            }

            song = list.Where(s => s.channel == (int)ChannelType.日语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                this.RYTileModel.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                this.RYTileModel.Title = ChannelType.日语.ToString();
                this.RYTileModel.Message = s.artist + "-" + s.title;
            }
            else
            {
                this.RYTileModel.Title = ChannelType.日语.ToString();
                this.RYTileModel.Message = "No Song!";
            }

            song = list.Where(s => s.channel == (int)ChannelType.粤语).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                this.YYTileModel.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                this.YYTileModel.Title = ChannelType.粤语.ToString();
                this.YYTileModel.Message = s.artist + "-" + s.title;
            }
            else
            {
                this.YYTileModel.Title = ChannelType.粤语.ToString();
                this.YYTileModel.Message = "No Song!";
            }

            song = list.Where(s => s.channel == (int)ChannelType.最爱).ToList();
            if (song != null && song.Count > 0)
            {
                var s = song[0];
                this.FAVTileModel.Source = new BitmapImage(new Uri(s.picture, UriKind.Absolute));
                this.FAVTileModel.Title = ChannelType.最爱.ToString();
                this.FAVTileModel.Message = s.artist + "-" + s.title;
            }
            else
            {
                this.FAVTileModel.Title = ChannelType.最爱.ToString();
                this.FAVTileModel.Message = "No Song!";
            }

            this.Title = "请选择一个你喜欢的频道";

        }

        /// <summary>
        /// 汉语
        /// </summary>
        private TileModel _HYTileModel;

        public TileModel HYTileModel
        {
            get { return _HYTileModel; }
            set { _HYTileModel = value; }
        }

        /// <summary>
        /// 欧美
        /// </summary>
        private TileModel _OMTileModel;

        public TileModel OMTileModel
        {
            get { return _OMTileModel; }
            set { _OMTileModel = value; }
        }

        /// <summary>
        /// 日语
        /// </summary>
        private TileModel _RYTileModel;

        public TileModel RYTileModel
        {
            get { return _RYTileModel; }
            set { _RYTileModel = value; }
        }

        /// <summary>
        /// 韩语
        /// </summary>
        private TileModel _HanYTileModel;

        public TileModel HanYTileModel
        {
            get { return _HanYTileModel; }
            set { _HanYTileModel = value; }
        }

        /// <summary>
        /// 粤语
        /// </summary>
        private TileModel _YYTileModel;

        public TileModel YYTileModel
        {
            get { return _YYTileModel; }
            set { _YYTileModel = value; }
        }


        /// <summary>
        /// 收藏
        /// </summary>
        private TileModel _FAVTileModel;

        public TileModel FAVTileModel
        {
            get { return _FAVTileModel; }
            set { _FAVTileModel = value; }
        }

        private RelayCommand<string> _TapCommand;

        public RelayCommand<string> TapCommand
        {
            get { return _TapCommand; }
            set
            {
                _TapCommand = value;
                this.RaisePropertyChanged("TapCommand");
            }
        }

        private RelayCommand _ChannelSelectCancelCommand;

        public RelayCommand ChannelSelectCancelCommand
        {
            get { return _ChannelSelectCancelCommand; }
            set { _ChannelSelectCancelCommand = value;
            this.RaisePropertyChanged("ChannelSelectedCancelCommand");
            }
        }

        private void HubTile_Tap(string hubTitle)
        {
            bool IsDo = false;
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                //点击
                if (gesture.GestureType == GestureType.Tap && !IsDo)
                {
                    IsDo = true;
                   // hubTitle = "华语";
                    if (!string.IsNullOrEmpty(hubTitle))
                    {
                        string pageUrl = "/View/MainPage.xaml?Channle=" + hubTitle;
                        NavigationHelper.NavigationMsgSend(pageUrl);
                      
                    }
                }
            }
        }

        private void NavigateToMainPage()
        {
            string pageUrl = "/View/MainPage.xaml";
            NavigationHelper.NavigationMsgSend(pageUrl);
                      
        }

        /// <summary>
        /// 导航完成后发生
        /// </summary>
        /// <param name="uri"></param>
        public void Navigated(Uri uri)
        {
            if (PlayListHelper.IsInited)
            {
               this.IsShowGoBtn = Visibility.Visible;
               this.PlayListHelper_InitChannelComplete();
            }
        }

        /// <summary>
        /// 获取对应的View的Url
        /// </summary>
        /// <returns></returns>
        public string GetViewUrl()
        {
            return "/View/ChannelTile.xaml";
        }
    }
}